<?php


include("config.php");
if (!isset($_SESSION["user"]) || $_SESSION["user"] == "") header('Location: index.php');

$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);

if ($db_www->isConnected()) {	
	$i_query = str_replace("<kodas>", $_SESSION['user'], $i_query);
	$minfo = $db_www->returnInfo($i_query);	
} else {
	die("Klientų aptarnavimo sistema laikinai neveikia.");
}

if (!$db_rdata->isConnected()) {	
	die("Klientų aptarnavimo sistema laikinai neveikia(rdata).");
}

$impav = "";

if (isset($_REQUEST["act"]) ){
	 if ($_REQUEST["act"] == "save") {		
			
			$kchange_query = str_replace("<pastabos>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pin"].";".$_REQUEST["pav"])), $kchange_query);
			$kchange_query = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["uid"])), $kchange_query);
			$kchange_query = str_replace("<kodas>", @iconv('UTF-8','windows-1257',$minfo["N08_kodas_ks"]), $kchange_query);

			$kchange_query_rdata = str_replace("<kortele>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["uid"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<nuo>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["nuo"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<iki>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["iki"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<credit>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["crlimit"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<p1>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pp1"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<p2>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pp2"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<p3>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pp3"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<p4>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pp4"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<p5>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pp5"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<p6>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pp6"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<p7>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["pp7"])), $kchange_query_rdata);
			$kchange_query_rdata = str_replace("<lustas>", "NULL", $kchange_query_rdata);
			
			$db_www->execute($kchange_query);
			$db_rdata->execute($kchange_query_rdata);
			
            logAction("Klientas koreguoja:" . $_REQUEST["uid"],3);
			$arr = explode(';',$_REQUEST["lst"]);
			$sql = "create table #tt_eilutes(kodas varchar(100));";
			$db_rdata->execute($sql);	
			foreach($arr as $item)
			{
				if ($item != "") {
					$sql = "insert into #tt_eilutes VALUES('".addslashes_mssql($item)."');";
					$db_rdata->execute($sql);				  
				}
			}
			$sql = "exec web_renew_groups "
			. "@kodas='".@iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["uid"]))."'";
			$db_rdata->execute($sql);			
			header("Location: cards.php");
			
		
	}
}	
include("header.php");
include("menu.php");




	if (isset($_REQUEST["uid"]) ) {
		$uid = @iconv('UTF-8','windows-1257',$_REQUEST["uid"]);
		$tit = "Kortelės redagavimas";	
		if (!isset($_POST["uid"])){
		
			$sql = "select * from n51_suth_c where N51_c_kodas_kt= '".addslashes_mssql($uid)."'";
			$info = $db_rdata->returnInfo($sql);	
		
			$sql = "SELECT case when (CHARINDEX (';',cast(N51_PASTABOS as varchar(8000)))=5) THEN
SUBSTRING(cast(N51_PASTABOS as varchar(8000)),6,1000) else case when (CHARINDEX (';',cast(N51_PASTABOS as varchar(8000)))=1) then SUBSTRING(cast(N51_PASTABOS as varchar(8000)),2,1000) else
cast(N51_PASTABOS as varchar(8000)) END END as N51_KORTA,
case when (CHARINDEX (';',cast(N51_PASTABOS as varchar(8000)))=5) then 
SUBSTRING(cast(N51_PASTABOS as varchar(8000)),1,4) 
else 
''
end as N51_PIN,n60_kodas_sm,n60_pav2,N51_PAV,CASE WHEN SUBSTRING(N51_PAV,1,1)='_'  THEN 0 ELSE 1 END as aktyvi FROM N60_asm a,N51_SUTH WHERE N51_KODAS_KS=a.N60_KODAS_KS AND N51_KODAS_KT = '".addslashes_mssql($uid)."' and n60_kodas_sm='".$minfo["n60_kodas_sm"]."'";
			$card = $db_www->returnInfo($sql);
		
			if (!empty($card)) {
				$pav = @iconv('windows-1257', 'UTF-8', $card["N51_KORTA"]);
				$pin = $card["N51_PIN"];
				$imcode = $minfo["N08_kodas_ks"];
				$impav = @iconv('windows-1257', 'UTF-8', trim($card["n60_pav2"]));
				$cardnr = @iconv('windows-1257', 'UTF-8', trim($card["N51_PAV"]));
				
			} else {
				header("Location: cards.php");
			}
				
			if (!empty($info)) {
				
				$crlimit = $info["N51_c_limitas"];
				$nuo = $info["n51_valanda_nuo"];
				$iki = $info["n51_valanda_iki"];
				$p1 = $info["n51_pirmadienis"];
				$p2 = $info["n51_antradienis"];
				$p3 = $info["n51_treciadienis"];
				$p4 = $info["n51_ketvirtadienis"];
				$p5 = $info["n51_penktadienis"];
				$p6 = $info["n51_sestadienis"];
				$p7 = $info["n51_sekmadienis"];
				
			} else {
				$crlimit = "";
				$nuo = "";
				$iki = "";
				$p1 = "0";
				$p2 = "0";
				$p3 = "0";
				$p4 = "0";
				$p5 = "0";
				$p6 = "0";
				$p7 = "0";
				
			}
		}
	} 
	

	



?>
<script>
function validateForm(myform)
{
	test();
	pin = document.getElementById("pin");
		if (!isUnsignedInteger(pin.value)) {
			alert("PIN kodas turi būti skaičius");
			return;
		}	
		if (pin.value.length == 0) pin.value = "0000";
		else if (pin.value.length == 1) pin.value = "000" + pin.value;
		else if (pin.value.length == 2) pin.value = "00" + pin.value;
		else if (pin.value.length == 3) pin.value = "0" + pin.value;
		
	credit = document.getElementById("crlimit").value.trim();
		if (credit != "") {
			if (!isUnsignedInteger(credit)) {
				alert("Neteisingas kredito limitas");
				return;
			}
		}	
	nuo = document.getElementById("nuo").value.trim();
	iki = document.getElementById("iki").value.trim();
		if (nuo != "") {
			if (!isUnsignedInteger(nuo)) {
				alert("Nuo valanda turi būti 0-24");
				return;
			} else {
				p = parseInt(nuo);
				if (p > 24 || p < 0){
					alert("Nuo valanda turi būti 0-24");
					return;				
				}
			}
		}
		if (iki != "") {
			if (!isUnsignedInteger(iki)) {
				alert("Iki valanda turi būti 0-24");
				return;
			} else {
				p = parseInt(iki);
				if (p > 24 || p < 0){
					alert("Iki valanda turi būti 0-24");
					return;				
				}
			}
		}	
	
	
	
	p1 = document.getElementById("p1");
	pp1 = document.getElementById("pp1");
	if (p1.checked==true) pp1.value = "1"; else pp1.value = "0";
	p2 = document.getElementById("p2");
	pp2 = document.getElementById("pp2");
	if (p2.checked==true) pp2.value = "1"; else pp2.value = "0";
	p3 = document.getElementById("p3");
	pp3 = document.getElementById("pp3");
	if (p3.checked==true) pp3.value = "1"; else pp3.value = "0";
	p4 = document.getElementById("p4");
	pp4 = document.getElementById("pp4");
	if (p4.checked==true) pp4.value = "1"; else pp4.value = "0";
	p5 = document.getElementById("p5");
	pp5 = document.getElementById("pp5");
	if (p5.checked==true) pp5.value = "1"; else pp5.value = "0";
	p6 = document.getElementById("p6");
	pp6 = document.getElementById("pp6");
	if (p6.checked==true) pp6.value = "1"; else pp6.value = "0";
	p7 = document.getElementById("p7");
	pp7 = document.getElementById("pp7");
	if (p7.checked==true) pp7.value = "1"; else pp7.value = "0";	
	myform.submit();
}

function isUnsignedInteger(s) {
  return (s.toString().search(/^[0-9]+$/) == 0);
}



</script>
<form method="post" name='forma' action='card_edit.php?act=save'>
<input id="pp1" type="hidden" name="pp1">
<input id="pp2" type="hidden" name="pp2">
<input id="pp3" type="hidden" name="pp3">
<input id="pp4" type="hidden" name="pp4">
<input id="pp5" type="hidden" name="pp5">
<input id="pp6" type="hidden" name="pp6">
<input id="pp7" type="hidden" name="pp7">

<input id="uid" type="hidden" name="uid" value="<?php echo @iconv('windows-1257','UTF-8',$uid);?>">
<input id="lst" type="hidden" name="lst" value="">
<table class="innercont" cellspacing="0" cellpadding="10" border="0">
<tr><td class="leftcol2">
<input class='but bgwhite nopad' type = "submit" value = "Išsaugoti" onclick="validateForm('forma');return false;"><br><br>
<Button id='unblockButton' class='but bgwhite nopad' onclick="window.location='cards.php';return false;">Atgal</Button>
</td><td class="centercol">


<div class="title2"><?php echo $tit?></div>
<table width="100%" border="0" cellspacing='0' cellpadding='0'>
<tr>
<td valign="top" colspan="2">
</td>
<td valign="top">
</td></tr>
<tr><td valign="top" colspan="2" class="fieldtitle">Kortelės numeris:</td><td valign="top" class="fieldtitle"></td></tr>
<tr>
<td valign="top" colspan="2">
<input style="width:275px;" id='cardnr' class="inp" type='text' name="cardnr" value="<?php echo htmlspecialchars($cardnr)?>" readonly="readonly">
</td>
<td valign="middle">

</td></tr>


<tr><td valign="top" colspan="2" class="fieldtitle">Kortelės pavadinimas:</td><td valign="top" class="fieldtitle"></td></tr>
<tr>
<td valign="top" colspan="2">
<input style="width:275px;" id='pav' class="inp" type='text' name="pav" value="<?php echo htmlspecialchars($pav)?>">
</td>
<td valign="top">

</td></tr>
<tr><td valign="top" colspan="2" class="fieldtitle">PIN kodas:</td><td valign="top" class="fieldtitle"></td></tr>
<tr>
<td valign="top" colspan="2">
<input name="pin" id="pin" class="inp" MAXLENGTH=4 type="text" value="<?php echo htmlspecialchars($pin);?>">
</td>
<td valign="top">
</td></tr>
<tr><td valign="top" colspan="2" class="fieldtitle">Mėn. kredito limitas:</td><td valign="top" class="fieldtitle"></td></tr>
<tr>
<td valign="top" colspan="2">
<input name="crlimit" id="crlimit" class="inp" MAXLENGTH=6  type="text" value="<?php echo htmlspecialchars($crlimit);?>">
</td>
<td valign="top">
</td></tr>
<tr><td valign="top" colspan="2" class="fieldtitle">Kortele leidžiama naudotis valandomis:</td><td valign="top" class="fieldtitle"></td></tr>
<tr>
<td valign="middle" colspan="2">
Nuo <input maxlength="2" type="text" id="nuo" name="nuo" class="inp" style="width:100px;" value="<?php echo htmlspecialchars($nuo);?>">
 Iki <input maxlength="2" id="iki" name="iki" type="text" class="inp" style="width:100px;" value="<?php echo htmlspecialchars($iki);?>">
</td>
<td valign="top">

</td></tr>
<tr><td valign="top" colspan="2" class="fieldtitle">Kortelės neaptarnauti savaitės dienomis:</td><td valign="top" class="fieldtitle"></td></tr>
<tr>
<td valign="middle" colspan="2">
<div style="text-align:left;margin-bottom:10px;">


<table cellpadding="0" cellspacing="0" border="0" style="border-collapse:collpase;width:275px;border: 1px solid #c2c2c2;">
<tr><th width="40" >I</th>
<th width="40" >II</th>
<th width="40" >III</th>
<th width="40" >IV</th>
<th width="40" >V</th>
<th width="40" >VI</th>
<th width="40" >VII</th></tr>
<tr>
<td style="text-align:center;"><input type="checkbox" id="p1" name="p1" <?php if ($p1 == "1") echo"checked";?>></td>
<td style="text-align:center;"><input type="checkbox" id="p2" name="p2" <?php if ($p2 == "1") echo"checked";?>></td>
<td style="text-align:center;"><input type="checkbox" id="p3" name="p3" <?php if ($p3 == "1") echo"checked";?>></td>
<td style="text-align:center;"><input type="checkbox" id="p4" name="p4" <?php if ($p4 == "1") echo"checked";?>></td>
<td style="text-align:center;"><input type="checkbox" id="p5" name="p5" <?php if ($p5 == "1") echo"checked";?>></td>
<td style="text-align:center;"><input type="checkbox" id="p6" name="p6" <?php if ($p6 == "1") echo"checked";?>></td>
<td style="text-align:center;"><input type="checkbox" id="p7" name="p7" <?php if ($p7 == "1") echo"checked";?>></td>
</tr>
</table>
</div>
<style>
#sortable1, #sortable2 { min-height:100px;list-style-type: none; margin: 0; padding: 0; float: left; margin-right: 10px; background: #eee; padding: 5px; width: 143px;}
#sortable1 li, #sortable2 li, { margin: 5px; padding: 5px; font-size: 1.2em; width: 120px; }
.bleft1 { padding:10px;border-left: 1px solid #cccccc;border-top: 1px solid #cccccc;border-bottom: 1px solid #cccccc;}
.bright1 { padding:10px;border-right: 1px solid #cccccc;border-top: 1px solid #cccccc;border-bottom: 1px solid #cccccc;}

</style>
<script>
$(function() {
$( "ul.droptrue" ).sortable({
connectWith: "ul"
});

$( "#sortable1, #sortable2" ).disableSelection();
});
function test (){
	sel =document.getElementById('sortable2');
	if (sel!=null){
	var items = sel.getElementsByTagName('li');
	lst =document.getElementById('lst');
	for (var i = 0; i < items.length; ++i) {
		lst.value = lst.value + ';'+ items[i].id.substring(1);
		
	}
	}
}
</script>



</td>
<td valign="top">
</td></tr>


<?php
if (isset($grupiu_sarasas)) {
$sql ="select * from [n52_sutd_c] where N52_c_kodas_kt='".addslashes_mssql($_REQUEST["uid"])."'";
$rgroups = $db_rdata->returnList($sql);	

$gr ='';
$ex ="''";
foreach ($rgroups as $item) {
	$gr .= '<li class="ui-state-default" id="v'.htmlspecialchars(trim($item["N52_c_kodas_gs"])).'">'.@iconv('windows-1257', 'UTF-8',htmlspecialchars(getGroupName($item["N52_c_kodas_gs"]))).'</li>';
	$ex .= ",'".$item["N52_c_kodas_gs"]."'";
}
$sql ="select * from n19_pgr where n19_tevas='".$grupiu_sarasas."' and n19_kodas_gs not in(".$ex.")";
$groups = $db_www->returnList($sql);
?>
<tr><td valign="top"  class="fieldtitle">Prekių grupių sąrašas:</td><td valign="top"  class="fieldtitle">Draudžiamų prekių grupių sąrašas</td></tr>
<tr>
<td valign="top" class='bleft1'>
<ul id="sortable1" class="droptrue">
<?php
foreach ($groups as $item) {
	echo '<li class="ui-state-default" id="v'.htmlspecialchars(trim($item["N19_KODAS_GS"])).'">'.@iconv('windows-1257', 'UTF-8',htmlspecialchars($item["N19_PAV"])).'</li>';

}
?>
</ul>
</td>
<td valign="top"  class='bright1'>


<ul id="sortable2" class="droptrue">
<?php
echo $gr;
?>
</ul>
</td></tr>
<?php
}
?>
</table>


</td><td class="rightcol">



</td></tr>
<tr><td>&nbsp;</td><td colspan="2">

</td></tr>
</table>
</form>
<?php

include("footer.php");
?>